<?php

namespace App\Services;
use App\Models\SystemConfig;

/**
 * 服务基类
 * Class BaseService
 * @package App\Services
 */
class BaseService
{
    protected $platform;

    public function __construct()
    {
        $this->platform = strtoupper(request()->header('platform',''));
    }

    /**
     * 成功返回
     * @param string $message
     * @param string $data
     * @return
     */
    public function success($data = '', $message = 'ok')
    {
        $version_config = SystemConfig::whereType(40)->first();//当前所有app版本号

        $platform = strtolower($this->platform);
        $version = '';
        if($platform == 'android'){
            $version = $version_config['config']['android'];
        }elseif($platform == 'wx'){
            $version = $version_config['config']['wxapp_version'];
        }elseif ($platform == 'ios'){
            $version = $version_config['config']['ios_version'];
        }
        response()->json([
            'status' => 200,
            'data' => $data,
            'msg' => $message,
            'time' => time(),
            'version'=>$version
        ], 200, ['charset' => 'utf-8'])->send();
    }

    /**
     * 失败返回
     * $errno
     * @param $status
     * @param string $message
     * @param string $data
     * @return
     */
    public function error($status, $message = '系统错误', $data = '')
    {
        response()->json([
            'status' => $status,
            'data' => $data,
            'msg' => $message,
            'time' => time()
        ], 200, ['charset' => 'utf-8'])->send();
    }

    /**
     * 错误返回
     * @param string $msg
     * @param string $data
     * @return mixed
     */
    public static function ierror($code, $msg = '', $data = '')
    {
        return self::returnFormat($code, $msg, $data);
    }

    /**
     * 正确返回
     * @param string $msg
     * @param string $data
     * @return mixed
     */
    public static function isuccess($msg = '', $data = '')
    {
        return self::returnFormat(0, $msg, $data);
    }

    /**
     * Service 基础返回，数据格式化，0正常，非0异常返回
     * @param $code
     * @param $msg
     * @param $data
     */
    public static function returnFormat($code, $msg, $data)
    {
        return array(
            'errno' => $code,
            'message' => $msg,
            'data' => $data,
        );
    }

}